/*----------------------------------------------------
*  This file is for account
-----------------------------------------------------*/
var _F = 'form', _S = '-submit', _P = 'account-';
var A = _P + 'register', _fA = A + _F, _btnA = A + _S;
var B = _P + 'login', _fB = B + _F, _btnB = B + _S;
var C = _P + 'activate', _fC = C + _F, _btnC = C + _S;
var D = _P + 'password-reset-request', _fD = D + _F, _btnD = D + _S;
var E = _P + 'password-reset', _fE = E + _F, _btnE = E + _S;
var F = _P + "change-password", _fF = F + _F, _bF = F + _S;
var G = _P + "update", _fG = G + _F, _bG = G + _S;
var OK = 'ok', _btn_name, _btn_val, _btn_msg = 'Sending...', _form_flag, _tip_msg = 'Loading...';

$(function(){
	/*---- from ------------------------------------*/
	$.common.ajaxForm($("#" + _fA + ", #" + _fB), $.ac.form.before, $.ac.form.success);
	$.common.ajaxForm($("#" + _fC + ", #" + _fD), $.ac.form2.before, $.ac.form2.success);
	$.common.ajaxForm($("#" + _fE + ", #" + _fF), $.ac.form3.before, $.ac.form3.success);
	$.common.ajaxForm($("#" + _fG), $.ac.upd.before, $.ac.upd.success);

	/*---- photo ------------------------------------*/
	$.ac.photo.init($('#mycarousel'));
	
	$('#captcha').bind({
		click: function(){captchaShow();},
		focus: function(){captchaShow();}
	});
	
	/*---- tips ------------------------------------*/
	$.ac.tips.init('input[type="submit"][id$="-submit"]');
	//$.ac.tab.init($(".profile .vlist li"));
});

captchaShow = function() {
	$('.img_captcha').show();
};

$.ac = {
	tips : {
		init: function(obj) {
			$(obj).poshytip({
				showOn: 'none',
				timeOnScreen : 3000,
				className: 'tip-yellowsimple',
				alignTo: 'target',
				alignX: 'center',
				alignY: 'top',
				offsetX: 5,
				content: $.ac.tips.updateCb
			});
		},
		updateCb : function () {
			return _tip_msg;
		},
		show : function(obj, msg) {
			_tip_msg = msg;
			$(obj).poshytip('show');
		}
	},
	v: {
		nickName : function(id, btn) {
			if (!$.common.test(/^\w{2,15}$/, $("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.AC_002);
				return false;
			}
			return true;
		},
		email : function(id, btn) {
			if (!$.common.email($("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.CO_001);
				return false;
			}
			return true;
		},
		name : function(id, btn) {
			if (!$.common.test(/^\w{2,15}$/, $("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.AC_003);
				return false;
			}
			return true;
		},
		oldPass : function(id, btn) {
			if (!$.common.test(/^\w{6,18}$/, $("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.AC_015);
				return false;
			}
			return true;
		},
		pass : function(id, btn) {
			if (!$.common.test(/^\w{6,18}$/, $("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.AC_005);
				return false;
			}
			return true;
		},
		confirmPass : function(id, btn) {
			if (!$.common.test(/^\w{6,18}$/, $("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.AB_001);
				return false;
			}
			return true;
		},
		passEquals : function(id, id2, btn) {
			if ($("#" + id).val() != $("#" + id2).val()) {
				$.ac.tips.show(btn, $.cde.msg.AB_002, null);
				return false;
			}
			return true;
		},
		passNotEquals : function(id, id2, btn) {
			if ($("#" + id).val() == $("#" + id2).val()) {
				$.ac.tips.show(btn, $.cde.msg.AC_018, null);
				return false;
			}
			return true;
		},
		photo : function(id, btn) {
			if ($("#" + id).val() === "-1") {
				$.ac.tips.show(btn, $.cde.msg.AC_006);
				return false;
			}
			return true;
		},
		captcha : function(id, btn) {
			if (!$.common.test(/^\w{4}$/, $("#" + id).val())) {
				$.ac.tips.show(btn, $.cde.msg.CP_001);
				return false;
			}
			return true;
		}
	},
	form : {
		validate : function(flag) {
			_b = flag ? _btnA : _btnB;
			var btn = $("#" + _b);
			var e = flag ? ($.ac.v.nickName("nickName", btn)
					&& $.ac.v.email("email", btn) && $.ac.v.name("name", btn)
					&& $.ac.v.pass("pass", btn)
					&& $.ac.v.confirmPass("confirmPass", btn)
					&& $.ac.v.passEquals("pass", "confirmPass", btn)
					&& $.ac.v.photo("photo", btn) && $.ac.v.captcha("captcha", btn))
					: 
					($.ac.v.name("name", btn) && $.ac.v.pass("pass", btn) && $.ac.v
							.captcha("captcha", btn));
			if (e) {
				$.ac.form.start();
			}
			return e;
		},
		before: function(formData){
			_form_flag = (formData.length > 4 );
			return $.ac.form.validate(_form_flag);//if true register else login
		},
		start: function() {
			_btn_val = $('#' + _btn).val();
			$.common.btnStatus(_btn, true, _btn_msg);
		},
		complete: function() {
			$.common.btnStatus(_btn, false, _btn_val);
		},
		success: function(data){
			$.ac.form.complete();
			if (OK === data) {
				$.ac.form.clear();
				if (_form_flag) {
					$('.form-tab').hide();$('.tips-tab').show();
				} else {
					window.location.href = BASE;
				}
			} else {
				$.ac.tips.show($('#' + _btn), $.cde.msg[data]);
			}
		},
		clear: function() {
			_form_name = _form_flag ? _formA : _formB;
			$('#' + _form_name).clearForm();
			if (_form_flag) $.ac.photo.disSelect();
		}
	},
	form2 : {
		validate : function(flag) {
			_btn = flag ? _btnC : _btnD;
			var btn = $("#" + _btn);
			var e = $.ac.v.email("email", btn) && $.ac.v.name("name", btn)
					&& $.ac.v.captcha("captcha", btn);
			if (e) {
				$.ac.form.start();
			}
			return e;
		},
		before: function(formData){
			_form_flag = (formData.length < 4 );
			return $.ac.form2.validate(_form_flag);//if true activate else null
		},
		success: function(data){
			$.ac.form.complete();
			if (OK === data) {
				if (_form_flag) {
				} else {
					$('.tips-tab').find('span').html($('#email').val());
				}
				$.ac.form2.clear();
				$('.form-tab').hide();
				$('.tips-tab').show();
			} else {
				$.ac.tips.show($('#' + _btn), $.cde.msg[data]);
			}
		},
		clear: function() {
			_form_name = _form_flag ? _formC : _formD;
			$('#' + _form_name).clearForm();
		}
	},
	form3 : {
		validate : function(flag) {
			_btn = flag ? _btnE : _btnF;
			var btn = $("#" + _btn);
			var e = flag ? ($.ac.v.pass("pass", btn)
					&& $.ac.v.confirmPass("confirmPass", btn)
					&& $.ac.v.passEquals("pass", "confirmPass", btn) && $.ac.v
					.captcha("captcha", btn))
					: 
					($.ac.v.oldPass("oldPass", btn)
					&& $.ac.v.pass("pass", btn)
					&& $.ac.v.confirmPass("confirmPass", btn)
					&& $.ac.v.passEquals("pass", "confirmPass", btn) && $.ac.v
					.passNotEquals("oldPass", "pass", btn));
			if (e) {
				$.ac.form.start();
			}
			return e;
		},
		before: function(formData){
			_form_flag = (formData.length > 0 );
			return $.ac.form3.validate(_form_flag);//if true activate else null
		},
		success: function(data){
			$.ac.form.complete();
			if (OK === data) {
				$.ac.form3.clear();
				$('.form-tab').hide();
				$('.tips-tab').show();
			} else {
				$.ac.tips.show($('#' + _btn), $.cde.msg[data]);
			}
		},
		clear: function() {
			_form_name = _form_flag ? _formE : null;
			$('#' + _form_name).clearForm();
		}
	},
	upd : {
		v : function(d) {
			_btn = d ? _btnG : null;
			var btn = $("#" + _btn);
			var e = d ? ($.ac.v.nickName("nickName", btn) && $.ac.v.photo(
					"photo", btn)) : null;
			if (e) {
				$.ac.form.start();
			}
			return e;
		},
		before : function(formData) {
			_form_flag = true;
			return $.ac.upd.v(_form_flag);
		},
		success : function(data) {
			$.ac.form.complete();
			if (OK === data) {
				$(".form-tab").hide();
				$(".tips-tab").show();
				$.ac.upd.html();
			} else {
				$.ac.tips.show($("#" + _btn ), $.cde.msg[d]);
			}
		},
		html : function() {
			b = $(".u_nickName"), s = $("#nickName").val();
			if (b.text() != s) {
				b.text(s);
			}
			b = $(".u_photo"), s = $("#photo").val(), c = b.find("span");
			if (c.text() != s) {
				c.text(s);
				t = b.find("img");
				v = t.attr("src");
				v = v.substring(0, v.lastIndexOf("/")) + "/";
				t.attr("src", v + s + ".gif");
			}
		}
	},
	photo : {
		size: 24,
		scroll : 3,
		start : function() {
			return parseInt($("#p-active").val());
		},
		list: function() {
			var s = $.ac.photo.size, p, ps = new Array(s), t; 
			for (var i = 0; i < s; i++){
				t = i + 1;
				if (t < 10) t = '0' + t;
				p = new Object();
				p.url = BASE + '/resources/themes/evernote/photo/' + t + '.gif';
				p.title = t;
				p.i = t;
				ps[i] = p;
			}
			return ps;
		},
		item: function(e) {
			return '<li title="' + e.title + '"i="' + e.i +'"><img src="' + e.url + '"alt="' + e.title + '"/></li>';
		},
		callback: function(cs, state) {
			var ps = $.ac.photo.list();
			for (var i = cs.first; i <= cs.last; i++) {
		       if (cs.has(i)) {continue;}
		       if (i > $.ac.photo.size) {break;}
		       cs.add(i, $($.ac.photo.item(ps[i - 1])).get(0));
		   }
		},
		disSelect: function(){
			var o = $('#mycarousel').find('li.p-active');
			o.removeClass('p-active');
			o.find('b.ico').remove();
		},
		select: function(o) {
			o.addClass('p-active').prepend('<b class="ico"></b>');
			$('#photo').val(o.attr('i'));
		},
		init: function(o) {
			$(o).jcarousel({
		        size: $.ac.photo.size,
		        scroll : $.ac.photo.scroll,
				start : $.ac.photo.start(),
		        itemLoadCallback: {onBeforeAnimation: $.ac.photo.callback}
		    });
			$(o).find('li').live('click', function(){
				$.ac.photo.disSelect();
				$.ac.photo.select($(this));
			});
			start = $.ac.photo.start();
			if (start > 0 && start < $.ac.photo.size) {
				$.ac.photo.select(d.find("li").eq(start - 1));
			}
		},
		tab : {
			init : function(tab) {
				tab.click(function() {
					$.ac.tab.cls(tab);
					$.ac.tab.click($(this));
					$.ac.tab.menu($(this));
				});
				$('div[class^="rel"]').not(":first").hide();
			},
			cls : function(tab) {
				tab.each(function(i) {
					if (i === 0) {
						$(this).addClass("first");
					}
					$(this).removeClass("none");
					$(this).removeClass("qactive");
				});
			},
			click : function(tab) {
				tab.prev().addClass("none");
				tab.addClass("qactive");
			},
			menu : function(tab) {
				$(".tips-tab").hide();
				$(".form-tab").show();
				rel = tab.find("a").attr("rel");
				$('div[class^="rel"]').hide();
				$('div[id="' + rel + '"]').show();
			},
		}
	}
};
